<html>
    <head>
        <title>Vidracaria</title>
        <script type="text/javascript" src="script/jquery-1.6.3.min.js"></script>
        <script type="text/javascript" src="script/jquery.maskedinput-1.3.min.js"></script>
        <script>
            jQuery(function($){
                $("#telefone").mask("(99) 9999-9999");
                $("#celular").mask("(99) 9999-9999");
                $("#fax").mask("(99) 9999-9999");
                $("#cep").mask("99.999.999");
                $("#cnpj").mask("99.999.999/9999-99");
            });
        </script>
    </head>
    <body>
        <div class="total">
            <?php
            include_once 'titulo.php';
            include_once 'cabecalho.php';
            include_once 'script/conecta.php';
            include_once 'MySQL.php';
            ?>
            <div class="cabecalho">
            </div>
            <?php
            include_once 'menuHorizontal.php';
            ?>
            <div class="conteudo">
                <div class="subconteudo">

                    <?php
                    if (empty($_POST) || $_POST['ACAO'] == "cadastraFornecedor") {
                        cadastraFornecedor();
                    }
                    ?>

                    <?php
                    if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'listaFornecedor') {
                        listaFornecedor($_POST['PESQUISA']);
                    }
                    ?>

                    <?php
                    if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'UpdateFornecedor') {
                        UpdateFornecedor();
                    }
                    ?>

                    <?php

                    function cadastraFornecedor() {
                        ?>

                        <form action="cadastro_fornecedor.php" method="POST">
                            <table class="tblPesquisar">
                                <tr><td><label class="label3">Procurar Fornecedor:</label>
                                        <input type="text" name="PESQUISA" size="50px" class="InputMaior"/>
                                        <input type="hidden" name="ACAO" value="listaFornecedor" />
                                        <input type="submit" value="" class="buttonPesquisar"/>
                            </table>
                        </form>


                        <form action="script/acoes.php" method="post" name="Cadastro" onSubmit="return ValidaFornecedor()">
                            <input type="hidden" name="ACAO" value="cadastraFornecedor">
                            <table class="tblForm">
                                <thead>
                                    <tr><td colspan="2">
                                            Cadastro de Fornecedor</td></tr>
                                </thead>
                                <tbody>
                                    <tr><td colspan="2">
                                <center>Preencha os campos abaixo para cadastrar cliente.<br/>
                                    Os campos com * são obrigatórios.</center></td></tr>

                                <tr><td>
                                        <label class="label">* Nome:</label></td><td>
                                        <input type = "text" name= "NOME" size="60px" maxlength="60"></td></tr>

                                <tr><td>
                                        <label class="label">Nome Fantasia:</label></td><td>
                                        <input type = "text" name= "FANTASIA" size="60px" maxlength="60"></td></tr>

                                <tr><td>
                                        <label class="label">CNPJ:</label></td><td>
                                        <input type = "text" name= "CNPJ" size="20px" id="cnpj" maxlength="14">
                                <tr><td>
                                        <label class="label">Telefone(1):</label></td><td>
                                        <input type = "text" name= "TELEFONE1" maxlength="13" onkeyup="mascaraTel(this)" id="telefone" size="10px"></td></tr>

                                <tr><td>
                                        <label class="label">Telefone(2)</label></td><td>
                                        <input type = "text" name= "TELEFONE2" maxlength="13" onkeyup="mascaraTel(this)" id="celular" size="10px"></td></tr>

                                <tr><td>
                                        <label class="label">Fax:</label></td><td>
                                        <input type = "text" name= "FAX" maxlength="13" onkeyup="mascaraTel(this)" id="fax" size="10px"></td></tr>

                                <tr><td>
                                        <label class="label">Contato:</label></td><td>
                                        <input type = "text" name= "CONTATO" size="20px" maxlength="40"></td></tr>
                                <tr><td>
                                        <label class="label">CEP:</label></td><td>
                                        <input type = "text" name= "CEP" maxlength="8" onkeyup="mascaraCep(this)" id="cep" size="10px"></td></tr>



                                <tr><td>
                                        <label class="label">Endereco:</label></td><td>
                                        <input type = "text" name= "ENDERECO" size="50px" maxlength="60">
                                        Nº<input type ="text" name="NUMERO" size="5" maxlength="6">
                                        Complemento<input type="text" name="COMPLEMENTO" size="15" maxlength="20"/></td></tr>

                                <tr><td>
                                        <label class="label">Bairro:</label></td><td>
                                        <input type = "text" name = "BAIRRO" size="25px" maxlength="40"/>

                                        <label>*UF:</label>
                                        <?php
                                        echo "<select name='UF'>";
                                        $sql = "SELECT *FROM tbl_uf;";
                                        $resultado = executaSQL($sql);
                                        while ($linha = mysqli_fetch_array($resultado)) {
                                            echo "<option value='" . $linha['uf_id'] . "'>" .
                                            $linha['uf_descricao'] . "</option>";
                                        }
                                        echo "</select>";
                                        ?>

                                        <label>*Cidade:</label>
                                        <?php
                                        echo "<select name='CIDADE'>";
                                        $sql = "SELECT *FROM tbl_cidade;";
                                        $resultado = executaSQL($sql);
                                        while ($linha = mysqli_fetch_array($resultado)) {
                                            echo "<option value='" . $linha['cid_id'] . "'>" . $linha['cid_nome'] . "</option>";
                                        }
                                        echo "</select>";
                                        ?>
                                        <br/></td></tr>

                                <tr><td>
                                        <label class="label">E-mail:</label></td><td>
                                        <input type = "text" name = "EMAIL" size="40px" maxlength="50"/></td></tr>

                                <tr><td>
                                        <label class="label"> Observação: </label></td><td>
                                        <textArea name="OBSERVACAO" maxlength="50"> </textArea> </td></tr>
                                </tbody>
                                <tfoot>

                                    <tr><td colspan="2" >

                                            <input type="submit" value="Cadastrar" class="blackButtonYes"/>
                                            <input type="reset" value="limpar" class="blackButtonNo"/>
                                        </td></tr>
                                </tfoot>
                            </table>

                        </form>
                        <?php
                    }
                    ?>

                    <?php

                    function listaFornecedor($pesquisa) {

                        //BOTAO NOVO FORNECEDOR
                        echo "<td>";
                        echo "<form action='cadastro_fornecedor.php' method='post'>";
                        echo "<input type='submit' value='Novo Fornecedor' class='btnAdicionar'>";
                        echo "</form>";


                        echo "<div style='width:98%; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto;'>";

                        $sql = "SELECT *FROM tbl_fornecedor WHERE for_nome LIKE '%".$pesquisa."%' ORDER BY for_id";
                        $resultado = executaSQL($sql);

                        //4-Consome o resultado. Se tiver
                        echo "<table border='1' class='tblUsuarios'>"; //crio uma tabela para exibiÃ§Ã£o de dados
                        echo "<caption>Tabela de Fornecedores.</caption>";

                        echo "<thead>";
                        echo "<th>" . "ID" . "</th>";
                        echo "<th>" . "Nome" . "</th>";
                        echo "<th>" . "Fantasia" . "</th>";
                        echo "<th>" . "CNPJ" . "</th>";
                        echo "<th>" . "Telefones" . "</th>";
                        echo "<th>" . "Contato" . "</th>";
                        echo "<th>" . "Endereço" . "</th>";
                        echo "<th colspan=2>" . "Ações" . "</th>";
                        echo "</thead>";

                        while ($linha = mysqli_fetch_array($resultado)) {
                            echo "<tr>";
                            echo "<td>" . $linha['for_id'] . "</td>";
                            echo "<td>" . $linha['for_nome'] . "</td>";
                            echo "<td>" . $linha['for_fantasia'] . "</td>";
                            echo "<td>" . $linha['for_cnpj'] . "</td>";
                            echo "<td>" . $linha['for_telefone'];
                            echo "<br/>" . $linha['for_telefone2'];
                            echo "<br/>" . $linha['for_fax'] . "</td>";
                            echo "<td>" . $linha['for_contato'] . "</td>";
                            echo "<td>" . $linha['for_endereco'] . " " . $linha['for_complemento'] . " " . $linha['for_numero'] . "</td>";
                            echo "<td>";

                            echo "<td>";
                            echo "<form method='post' action='script/acoes.php' onSubmit='return Confirma Exclusao?()'>"; //form de excluir
                            echo "<input type='submit' value='' class='btnExcluir'/>"; //botão de excluir
                            echo "<input type='hidden'  name='ACAO' value='deleteFornecedor'/>";
                            echo "<input type='hidden' name='ID' value='" . $linha["for_id"] . "'/>";
                            echo "</form>";

                            echo "<form action='cadastro_fornecedor.php' method='post'>";
                            echo "<input type='hidden' name='ACAO' value='UpdateFornecedor'>";
                            echo "<input type='hidden' name='ID' value='" . $linha['for_id'] . "'>";
                            echo "<input type='hidden' name='NOME' value='" . $linha['for_nome'] . "'>";
                            echo "<input type='hidden' name='FANTASIA' value='" . $linha['for_fantasia'] . "'>";
                            echo "<input type='hidden' name='CNPJ' value='" . $linha['for_cnpj'] . "'>";
                            echo "<input type='hidden' name='TELEFONE1' value='" . $linha['for_telefone'] . "'>";
                            echo "<input type='hidden' name='TELEFONE2' value='" . $linha['for_telefone2'] . "'>";
                            echo "<input type='hidden' name='FAX' value='" . $linha['for_fax'] . "'>";
                            echo "<input type='hidden' name='CONTATO' value='" . $linha['for_contato'] . "'>";
                            echo "<input type='hidden' name='CEP' value='" . $linha['for_cep'] . "'>";
                            echo "<input type='hidden' name='ENDERECO' value='for_endereco'>";
                            echo "<input type='hidden' name='NUMERO' value='" . $linha['for_numero'] . "'>";
                            echo "<input type='hidden' name='COMPLEMENTO' value='" . $linha['for_complemento'] . "'>";
                            echo "<input type='hidden' name='UF' value='" . $linha['fk_uf_id'] . "'>";
                            echo "<input type='hidden' name='CIDADE' value='" . $linha['fk_cid_id'] . "'>";
                            echo "<input type='hidden' name='EMAIL' value='" . $linha['for_email'] . "'>";
                            echo "<input type='hidden' name='OBSERVACAO' value='" . $linha['for_observacao'] . "'>";
                            echo "<input type='submit' value='' class='btnEditar'>";
                            echo "</form>";


                            echo "</tr>";
                        }
                        echo "</table>";
                    }
                    ?>

                    <?php

                    function UpdateFornecedor() {
                        ?>

                        <form action="cadastro_fornecedor.php" method="POST">
                            <table class="tblPesquisar">
                                <tr><td><label class="label3">Procurar Fornecedor:</label>
                                        <input type="text" name="PESQUISA" size="50px" class="InputMaior"/>
                                        <input type="hidden" name="ACAO" value="listaFornecedor" />
                                        <input type="submit" value="" class="buttonPesquisar"/>
                            </table>
                        </form>


                        <form action="script/acoes.php" method="post" name="Cadastro" onSubmit="return ValidaFornecedor()">
                            <input type="hidden" name="ACAO" value="UpdateFornecedor">
                            <table class="tblForm">
                                <thead>
                                    <tr><td colspan="2">
                                            Cadastro de Fornecedor</td></tr>
                                </thead>
                                <tbody>
                                    <tr><td colspan="2">
                                <center>Preencha os campos abaixo para cadastrar cliente.<br/>
                                    Os campos com * são obrigatórios.</center></td></tr>

                                <input type="hidden" name="ID"
                                <?php
                                if ($_POST['ID']) {
                                    echo " value=\"";
                                    echo $_POST['ID'];
                                    echo "\" ";
                                }
                                ?>/>
                                <tr><td>
                                        <label class="label">* Nome:</label></td><td>
                                        <input type = "text" name= "NOME" size="60px" maxlength="60"
                                        <?php
                                        if ($_POST['NOME']) {
                                            echo" value=\"";
                                            echo $_POST['NOME'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>

                                <tr><td>
                                        <label class="label">Nome Fantasia:</label></td><td>
                                        <input type = "text" name= "FANTASIA" size="60px" maxlength="60"
                                        <?php
                                        if ($_POST['FANTASIA']) {
                                            echo" value=\"";
                                            echo $_POST['FANTASIA'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>

                                <tr><td>
                                        <label class="label">CNPJ:</label></td><td>
                                        <input type = "text" name= "CNPJ" size="20px" id="cnpj" maxlength="14"
                                        <?php
                                        if ($_POST['CNPJ']) {
                                            echo" value=\"";
                                            echo $_POST['CNPJ'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>
                                <tr><td>
                                        <label class="label">Telefone(1):</label></td><td>
                                        <input type = "text" name= "TELEFONE1" maxlength="13" onkeyup="mascaraTel(this)" id="telefone" size="10px"
                                        <?php
                                        if ($_POST['TELEFONE1']) {
                                            echo" value=\"";
                                            echo $_POST['TELEFONE'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>

                                <tr><td>
                                        <label class="label">Telefone(2)</label></td><td>
                                        <input type = "text" name= "TELEFONE2" maxlength="13" onkeyup="mascaraTel(this)" id="celular" size="10px"
                                        <?php
                                        if ($_POST['TELEFONE2']) {
                                            echo" value=\"";
                                            echo $_POST['TELEFONE2'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>

                                <tr><td>
                                        <label class="label">Fax:</label></td><td>
                                        <input type = "text" name= "FAX" maxlength="13" onkeyup="mascaraTel(this)" id="fax" size="10px"
                                        <?php
                                        if ($_POST['FAX']) {
                                            echo" value=\"";
                                            echo $_POST['FAX'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>
                                <tr><td>
                                        <label class="label">Contato:</label></td><td>
                                        <input type = "text" name= "CONTATO" size="20px" maxlength="40"
                                        <?php
                                        if ($_POST['CONTATO']) {
                                            echo" value=\"";
                                            echo $_POST['CONTATO'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>
                                <tr><td>
                                        <label class="label">CEP:</label></td><td>
                                        <input type = "text" name= "CEP" maxlength="8" onkeyup="mascaraCep(this)" id="cep" size="10px"></td></tr>



                                <tr><td>
                                        <label class="label">Endereco:</label></td><td>
                                        <input type = "text" name= "ENDERECO" size="50px" maxlength="60"
                                        <?php
                                        if ($_POST['ENDERECO']) {
                                            echo" value=\"";
                                            echo $_POST['ENDERECO'];
                                            echo "\" ";
                                        }
                                        ?>>
                                        Nº<input type ="text" name="NUMERO" size="5" maxlength="6"
                                        <?php
                                        if ($_POST['NUMERO']) {
                                            echo" value=\"";
                                            echo $_POST['NUMERO'];
                                            echo "\" ";
                                        }
                                        ?>>
                                        Complemento<input type="text" name="COMPLEMENTO" size="15" maxlength="20"
                                        <?php
                                        if ($_POST['COMPLEMENTO']) {
                                            echo" value=\"";
                                            echo $_POST['COMPLEMEMTO'];
                                            echo "\" ";
                                        }
                                        ?>></td></tr>

                                <tr><td>
                                        <label class="label">Bairro:</label></td><td>
                                        <input type = "text" name = "BAIRRO" size="25px" maxlength="40"/>

                                        <label>*UF:</label>
                                        <?php
                                        $sql = "SELECT *FROM tbl_uf;";
                                        echo "<select name='UF' required>";
                                        echo "<option value=''>UF </option>";
                                        $resposta = executaSQL($sql);
                                        while ($linha = mysqli_fetch_array($resposta)) {
                                            ?>
                                    <option value="<?php echo $linha["uf_id"]; ?>" <?php
                                    if ($linha["uf_id"] == $_POST['UF']) {
                                        echo "SELECTED";
                                    } else {
                                        echo "";
                                    };
                                            ?>><?php echo $linha["uf_descricao"]; ?></option>

                                    <?php
                                }
                                echo "</select>";
                                ?>


                                <label>*Cidade:</label>
                                <?php
                                $sql = "SELECT *FROM tbl_cidade;";
                                echo "<select name='CIDADE' required>";
                                echo "<option value=''>CIDADE </option>";
                                $resposta = executaSQL($sql);
                                while ($linha = mysqli_fetch_array($resposta)) {
                                    ?>
                                    <option value="<?php echo $linha["cid_id"]; ?>" <?php
                            if ($linha["cid_id"] == $_POST['CIDADE']) {
                                echo "SELECTED";
                            } else {
                                echo "";
                            };
                                    ?>><?php echo $linha["cid_nome"]; ?></option>

                                    <?php
                                }
                                echo "</select>";
                                ?>
                                <br/></td></tr>

                                <tr><td>
                                        <label class="label">E-mail:</label></td><td>
                                        <input type = "text" name = "EMAIL" size="40px" maxlength="50"/></td></tr>

                                <tr><td>
                                        <label class="label"> Observação: </label></td><td>
                                        <textArea name="OBSERVACAO" maxlength="50"> </textArea> </td></tr>
                                </tbody>
                                <tfoot>

                                    <tr><td colspan="2" >

                                            <input type="submit" value="Cadastrar" class="blackButtonYes"/>
                                            <input type="reset" value="limpar" class="blackButtonNo"/>
                                        </td></tr>
                                </tfoot>
                            </table>

                        </form>
                        <?php
                    }
                    ?>



                </div>
            </div>
            <div class="rodape">
                <?php
                include_once 'rodape.php';
                ?>
            </div>
        </div>
    </body>
</html>

<?php

function executaSQL($sql) {
    $mySQL = new MySQL;
    $resultado = $mySQL->execSP($sql);
    return $resultado;
}
?>